import {classicBehavior} from '../classic-behavior'

const mMgr = wx.getBackgroundAudioManager()
Component({
  /**
   * 组件的属性列表
   */
  behaviors:[classicBehavior],
  properties: {
    src: String,
    title:String
  },
  data: {
    playing: false,
    waittingUrl: 'images/player@waitting.png',
    playingUrl: 'images/player@playing.png'
  },
  attached(){
    // 组件加载时判断音乐是否正在播放和是否播放的是当前音乐
    this._judgeStatus()

    this._listenMgr()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onPlay(){
      if(!this.data.playing){
        this.setData({
          playing: true
        })
        mMgr.title = this.properties.title
        mMgr.src = this.properties.src
      }else{
        this.setData({
          playing: false
        })
        mMgr.pause()
      }
    },
    _judgeStatus(){
      if(mMgr.paused){
        this.setData({
          playing: false
        })
        return
      }

      if(mMgr.src == this.properties.src){
        this.setData({
          playing: true
        })
      }
    },
    _listenMgr(){
      mMgr.onPlay(() => {
        this._judgeStatus()
      })
      mMgr.onPause(() => {
        this._judgeStatus()
      })
      mMgr.onStop(() => {
        this._judgeStatus()
      })
      mMgr.onEnded(() => {
        this._judgeStatus()
      })
    }
  }
})
